﻿using jiaju.Areas.Back.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace jiaju.Areas.Back.Models.DAL
{
    public class ProductTypeService
    {
        
        public static List<ProductType> getProductTypeList(int page, int limit, string name)
        {
            string sql = string.Format("select top({0}) * from dbo.productType where Id not in (select top(({1}-1) * {0}) Id from dbo.productType)", limit, page);
            if (!string.IsNullOrEmpty(name))
            {
              
                sql = sql + string.Format(" and Name like '%{0}%'", name);
            }         
            List<ProductType> list = new List<ProductType>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                ProductType productType = new ProductType()
                {
                    Id= Id,
                    Name = Name
                };
                list.Add(productType);
            }
            return list;
        }
        public static int AddProductType(ProductType art)
        {
            string sql = string.Format(@"insert into dbo.productType
            (Name) values('{0}')",art.Name);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        public static ProductType GetById(string ArtId)
        {
            string sql = "select * from dbo.productType where Id=" + ArtId;
            ProductType article = null;
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];               
                article = new ProductType()
                {
                    Id = Id,
                    Name = Name,                   
                };
            }
            return article;
        }
        public static int EditProductType(ProductType art)
        {
            String sql = string.Format(@"UPDATE dbo.productType
                                      SET Name ='{0}'                                     
                                       WHERE Id={1}
                                 ", art.Name,art.Id);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        public static int DelProductType(String Id)
        {
            String sqlPhoto =String.Format("delete from dbo.picture where ProductId in(select Id from dbo.product where ProductTypeId={0})",Id);
            DBHelp.ExecuteNonQuery(sqlPhoto);
            String sqlRec = "delete from dbo.product where ProductTypeId=" + Id;
            DBHelp.ExecuteNonQuery(sqlRec);
            string sql = "delete from dbo.productType  where Id=" + Id;
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        public static List<ProductType> getAllName()
        {
            string sql = "select * from dbo.productType";
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<ProductType> list = new List<ProductType>();
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                ProductType arc = new ProductType() { Id = id, Name = Name };
                list.Add(arc);
            }
            return list;
        }

    }
}